package com.xilin.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xilin.wiki.domain.Content;
import com.xilin.wiki.domain.Doc;
import com.xilin.wiki.domain.DocExample;
import com.xilin.wiki.mapper.ContentMapper;
import com.xilin.wiki.mapper.DocMapper;
import com.xilin.wiki.req.DocQueryReq;
import com.xilin.wiki.req.DocSaveReq;
import com.xilin.wiki.response.DocQueryResp;
import com.xilin.wiki.response.PageResp;
import com.xilin.wiki.util.CopyUtil;
import com.xilin.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DocService {
    private static final Logger LOG = LoggerFactory.getLogger(DocService.class);

    @Resource
    private DocMapper docMapper;

    @Resource
    private ContentMapper contentMapper;
    @Resource
    private SnowFlake snowFlake;

    public PageResp<DocQueryResp> list(DocQueryReq req) {

        DocExample docExample = new DocExample();
        docExample.setOrderByClause("sort asc");
        //内部类 where查询
        DocExample.Criteria criteria = docExample.createCriteria();
        //判断是否查询单个数据，否则全部展示


        PageHelper.startPage(req.getPage(), req.getSize());
        //查sql获得的<Doc> list
        List<Doc> list = docMapper.selectByExample(docExample);
        PageInfo<Doc> pageInfo = new PageInfo<>(list);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());
        //List<Doc> --> List<DocResp>
        List<DocQueryResp> respList = new ArrayList<>();

       /* for (Doc doc : list) {
          //DocResp docResp = new DocResp();
          //  BeanUtils.copyProperties(doc,docResp);
          对象复制
            DocResp docResp = CopyUtil.copy(doc, DocResp.class);
            respList.add(docResp);
        }*/

        //列表复制
        List<DocQueryResp> list1 = CopyUtil.copyList(list, DocQueryResp.class);

        PageResp<DocQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list1);
        return pageResp;
    }

    public List<DocQueryResp> all(Long ebookId) {
        DocExample docExample = new DocExample();
        docExample.createCriteria().andEbookIdEqualTo(ebookId);
        docExample.setOrderByClause("sort asc");
        List<Doc> list = docMapper.selectByExample(docExample);
        //列表复制
        List<DocQueryResp> list1 = CopyUtil.copyList(list, DocQueryResp.class);

        return list1;
    }

    //保存文档
    public void save(DocSaveReq req) {
        Doc doc = CopyUtil.copy(req, Doc.class);
        Content content = CopyUtil.copy(req, Content.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            //文档表新增
            doc.setId(snowFlake.nextId());
            docMapper.insert(doc);
            //富文本表新增
            content.setId(doc.getId());
            contentMapper.insert(content);
        } else {
            docMapper.updateByPrimaryKey(doc);
            int count = contentMapper.updateByPrimaryKeyWithBLOBs(content);
            if (count == 0) {
                contentMapper.insert(content);
            }
        }
    }

    public String findContent(Long id) {
        Content content = contentMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(content)){
            return "";
        }else {
            return content.getContent();
        }

    }


    public void delete(Long id) {
        docMapper.deleteByPrimaryKey(id);
    }

    public void delete(List<String> ids) {
        DocExample docExample = new DocExample();
        DocExample.Criteria criteria = docExample.createCriteria();
        criteria.andIdIn(ids);
        docMapper.deleteByExample(docExample);
    }

}
